#include <iostream>
#include <vector>
#include <string>
#include <set>
#include <sw/redis++/redis++.h>
#include "until.hpp"
#include <thread>
using namespace std;
using namespace chrono_literals;


void test1(sw::redis::Redis& redis)
{
    cout << "test sadd smembers" << endl;
    redis.flushall();

    redis.sadd("key1","111");

    redis.sadd("key1",{"222","333"});

    vector<string> vs{"444","555","666"};
    redis.sadd("key1",vs.begin(),vs.end());

    set<string> ret;
    std::insert_iterator<set<string>> it(ret,ret.end());

    redis.smembers("key1",it);
    PrintElenums(ret);
}

void test2(sw::redis::Redis& redis)
{
    cout << "test sismember scard" << endl;
    redis.flushall();

    redis.sadd("key1",{"111","222","333"});

    auto ret = redis.sismember("key1","222");
    cout << ret << endl;

    ret = redis.sismember("key1","999");
    cout << ret << endl;

    auto sum = redis.scard("key1");
    cout << sum << endl;
}

void test3(sw::redis::Redis& redis)
{
    cout << "test spop" << endl;
    redis.flushall();

    redis.sadd("key1",{"111","222","333"});
    //随机删除key中的一个元素
    redis.spop("key1");

    set<string> mems;
    insert_iterator<set<string>> itm(mems,mems.end());
    redis.smembers("key1",itm);
    PrintElenums(mems);
}

void test4(sw::redis::Redis& redis)
{
    cout << "test sinter" << endl;
    redis.flushall();

    redis.sadd("key1",{"111","222","333"});
    redis.sadd("key2",{"444","555","333"});

    set<string> ret;
    insert_iterator<set<string>> it(ret,ret.end());

    redis.sinter({"key1","key2"},it);

    PrintElenums(ret);
}

void test5(sw::redis::Redis& redis)
{
    cout << "test sinterstore" << endl;
    redis.flushall();
    
    redis.sadd("key1",{"111","222","333"});
    redis.sadd("key2",{"444","555","333"});

    redis.sinterstore("key3",{"key1","key2"});

    set<string> ret;
    std::insert_iterator<set<string>> it(ret,ret.end());

    redis.smembers("key3",it);
    PrintElenums(ret);
}
int main()
{
    sw::redis::Redis redistmp("tcp://127.0.0.1:6379");
    // test1(redistmp);
    // test2(redistmp);
    //test3(redistmp);
    //test4(redistmp);
    test5(redistmp);
    
    return 0;
}